package com.example.blogother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blogother.mapper.ArticleMapper;
import com.example.blogother.mapper.FavoriteMapper;
import com.example.blogother.mapper.UserinfoMapper;
import com.xpd.blog.bean.Article;
import com.xpd.blog.bean.Favorite;
import com.xpd.blog.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xpd
 * @since 2024-09-29
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Autowired
    FavoriteMapper favoriteMapper;
    @Autowired
    ArticleMapper articleMapper;
    //查找该博主有没有收藏blog
    @RequestMapping("selectFav")
    public Result selectFav(@RequestParam int usid,@RequestParam int aid){
        Favorite favorite = favoriteMapper.selectByUsidAndId(usid, aid);
        return new Result(1,"查询成功",favorite);
    }
    //添加收藏
    @RequestMapping("addFav")
    public Result addFav(@RequestParam int usid,@RequestParam int aid){
        Favorite favorite=new Favorite();
        favorite.setAid(aid);
        favorite.setUsid(usid);
        favorite.setTime(LocalDateTime.now());
        int insert = favoriteMapper.insert(favorite);
        if(insert>0){
            return new Result(1,"收藏成功",null);
        }else{
            return new Result(-1,"收藏失败",null);
        }
    }
    //删除收藏
    @RequestMapping("delFav")
    public Result delFav(@RequestParam int usid,@RequestParam int aid){
        Favorite favorite=new Favorite();
        int delete = favoriteMapper.delFavByusidAndId(usid, aid);
        if(delete>0){
            return new Result(1,"取消成功",null);
        }else{
            return new Result(-1,"取消失败",null);
        }
    }

    //查看自己的收藏
    @RequestMapping("getFav")
    public Result getFav(@RequestParam int usid,@RequestParam int page,@RequestParam int size){
        List<Favorite> favorites = favoriteMapper.selectByUsidArticles(usid,((page-1)*5),size);
        LambdaQueryWrapper<Favorite> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Favorite::getUsid,usid);
        List<Favorite> favorites1 = favoriteMapper.selectList(lqw);
        return new Result(1,favorites1.size()+"",favorites);
    }
}

